package com.cn.jx.manager.service.mtn.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.enumType.JobGridEnum;
import com.cn.jx.common.enumType.OperatLogGridEnum;
import com.cn.jx.common.enumType.OrgGridEnum;
import com.cn.jx.common.enumType.ResourceGridEnum;
import com.cn.jx.common.enumType.RoleGridEnum;
import com.cn.jx.common.enumType.UserGridEnum;
import com.cn.jx.common.enumType.UserOrgJobGridEnum;
import com.cn.jx.common.enumType.cityGridEnum;
import com.cn.jx.common.enumType.dictDataGridEnum;
import com.cn.jx.common.enumType.dictTypeGridEnum;
import com.cn.jx.common.enumType.districtGridEnum;
import com.cn.jx.common.enumType.emailGridEnum;
import com.cn.jx.common.enumType.noticeGridEnum;
import com.cn.jx.common.enumType.provinceGridEnum;
import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.DataGridRepository;
import com.cn.jx.manager.po.DataGrid;
import com.cn.jx.manager.service.mtn.IDataGridServiceMtn;
import com.jx.enumType.DataGrideTypeEnum;

@Service("dataGridServiceMtn") 
@Transactional
public class DataGridServiceImplMtn extends BaseService implements IDataGridServiceMtn {

	@Autowired
	private DataGridRepository dataGridDao;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void initDataGrid() {
		// 角色动态列
		saveDataGridList(DataGrideTypeEnum.JOB_MGR);
		// 角色动态列
		saveDataGridList(DataGrideTypeEnum.ORG_MGR);
		// 角色动态列
		saveDataGridList(DataGrideTypeEnum.RESOURCE_MGR);
		// 角色动态列
		saveDataGridList(DataGrideTypeEnum.ROLE_MGR);
		// 角色动态列
		saveDataGridList(DataGrideTypeEnum.USER_MGR);
		// 角色动态列
		saveDataGridList(DataGrideTypeEnum.USER_ORG_JOB);
		// 操作日志
		saveDataGridList(DataGrideTypeEnum.OPERT_LOG);
		// 城市
		saveDataGridList(DataGrideTypeEnum.PROVINCE);
		saveDataGridList(DataGrideTypeEnum.CITY);
		saveDataGridList(DataGrideTypeEnum.DISTRICT);
		saveDataGridList(DataGrideTypeEnum.COMMUNITY);
		// 字典
		saveDataGridList(DataGrideTypeEnum.DICT_TYPE);
		saveDataGridList(DataGrideTypeEnum.DICT_DATA);
		//通知
		saveDataGridList(DataGrideTypeEnum.NOTICE_MGR);
		
		//邮件
		saveDataGridList(DataGrideTypeEnum.MESSAGE_MGR);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveDataGridList(DataGrideTypeEnum type) {
		List<DataGrid> list=null;
		List<String> fieldList=dataGridDao.findFieldByType(type.getId());
		switch (type) {
		case JOB_MGR:
			list=JobGridEnum.getAllEnum(type,fieldList);
			break;
		case ORG_MGR:
			list=OrgGridEnum.getAllEnum(type,fieldList);
			break;
		case RESOURCE_MGR:
			list=ResourceGridEnum.getAllEnum(type,fieldList);
			break;
		case ROLE_MGR:
			list=RoleGridEnum.getAllEnum(type,fieldList);
			break;
		case USER_MGR:
			list=UserGridEnum.getAllEnum(type,fieldList);
			break;
		case USER_ORG_JOB:
			list=UserOrgJobGridEnum.getAllEnum(type,fieldList);
			break;
		case OPERT_LOG:
			list=OperatLogGridEnum.getAllEnum(type,fieldList);
			break;
		case PROVINCE:
			list=provinceGridEnum.getAllEnum(type,fieldList);
			break;
		case CITY:
			list=cityGridEnum.getAllEnum(type,fieldList);
			break;
		case DISTRICT:
			list=districtGridEnum.getAllEnum(type,fieldList);
			break;
		case COMMUNITY:
			list=districtGridEnum.getAllEnum(type,fieldList);
			break;
		case DICT_TYPE:
			list=dictTypeGridEnum.getAllEnum(type,fieldList);
			break;
		case DICT_DATA:
			list=dictDataGridEnum.getAllEnum(type,fieldList);
			break;
		case NOTICE_MGR:
			list=noticeGridEnum.getAllEnum(type,fieldList);
			break;
		case MESSAGE_MGR:
			list=emailGridEnum.getAllEnum(type,fieldList);
			break;
		default:
			break;
		}
		if(list!=null && list.size()>0){
			for(DataGrid dg:list){
				dataGridDao.saveAndFlush(dg);
			}
		}
	}

}
